import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/api/odoo/mrp_production_odoo_api.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/models/odoo/mrp_production.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';

class PriceTrendAdd extends StatefulWidget {
  PriceTrendAdd({this.mrpProduction});

  final MrpProduction? mrpProduction;

  @override
  _PriceTrendAddState createState() => _PriceTrendAddState();
}

class _PriceTrendAddState extends State<PriceTrendAdd> {
  late MrpProduction mrpProduction;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  @override
  void initState() {
    super.initState();
    this.isAdd = widget.mrpProduction == null;
    this.mrpProduction = widget.mrpProduction ?? MrpProduction();
    _loadSelectVO();
  }

  _loadSelectVO() async {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CryInput(
            label: '产品名称',
            value: mrpProduction.productName,
            onSaved: (v) {
              mrpProduction.productName = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '产品编码',
            value: mrpProduction.productCode,
            onSaved: (v) {
              mrpProduction.productCode = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '规格型号',
            value: mrpProduction.productDesc,
            onSaved: (v) {
              mrpProduction.productDesc = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '产品分类',
            value: mrpProduction.productType,
            onSaved: (v) {
              mrpProduction.productType = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '计量单位',
            value: mrpProduction.productUom,
            onSaved: (v) {
              mrpProduction.productUom = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          // CryInput(
          //   label: '安全数量',
          //   value: mrpProduction.quantity,
          //   onSaved: (v) {
          //     mrpProduction.quantity = v;
          //   },
          //   validator: (v) {
          //     return !isBlank(v)?double.parse(v!) == 0 ? "安全数量不能为0" : null:"必填";
          //   },
          // ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            MrpProductionOdooApi.addProduct({
              'product_name': mrpProduction.productName,
              'product_default_code': mrpProduction.productCode,
              'product_desc': mrpProduction.productDesc,
              'product_type': mrpProduction.productType,
              'uom_name': mrpProduction.productUom,
            }).then((ResponseBodyApi res) {
              if (!res.success!) {
                //package:flutter_admin/pages/common/cry(re s.message!);
                return;
              }
              Navigator.pop(context, mrpProduction.productCode);
              CryUtils.message(S.of(context).saved);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd! ? S.of(context).add : S.of(context).modify),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
